//
//  BackViewController.m
//  PlayBall
//
//  Created by Huawen Liu on 12-8-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddBackViewController.h"
#import "JddCommonFunction.h"
#import "JddGlobalParameter.h"
#import "JddDataParser.h"
#import "JddError.h"
#import "JddUserProfile.h"
#import "JddViewControllerManager.h"

@interface JddBackViewController ()

@end

@implementation JddBackViewController

@synthesize backMessageField;
@synthesize clearButton;
@synthesize submitButton;
@synthesize bgScrollView;
//@synthesize backButton;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddBackViewController);


- (BOOL)textViewShouldBeginEditing:(UITextField *)textView{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    
    [self.bgScrollView setContentOffset:CGPointMake(0,0)animated:YES];
   
    self.penImg.hidden = YES;
    self.tiShiLabel.hidden = YES;
    
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text length] == 0 || backMessageField.text == nil) {
        self.penImg.hidden = NO;
        self.tiShiLabel.hidden = NO;
    }
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:submitButton]) {
        return NO;
    }
    
    return YES;
}

- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    [backMessageField resignFirstResponder];
    
    [self.bgScrollView removeGestureRecognizer:sender];
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        backMessageField.text = @"";
        self.penImg.hidden = NO;
        self.tiShiLabel.hidden = NO;
    }
}

-(void)QueryBackOperationResult:(NSData *)result{
    if(result){
        if ([JddDataParser parseBindMobileResult:result]) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }
}

- (IBAction)registerButtonClicked:(id)sender{
    if ([backMessageField.text length] == 0) {
        [JddCommonFunction messageBoxWithMessage:@"请输入反馈意见"];
        return;
    }        
    JddQueryBackOperation *operation = [[JddQueryBackOperation alloc] init];
    operation.delegate = self;
    operation.BackMessage = backMessageField.text;
    operation.Contact = @"";
    [[[JddGlobalParameter sharedInstance] operationQueue] addOperation:operation];
}

- (IBAction)clearButtonClicked:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确认清空反馈意见？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
}



-(IBAction)returnTextField:(id)sender{
    [backMessageField resignFirstResponder];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   // [self.navigationItem setTitle:@"反馈意见"];

    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"反馈意见"];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
//    //self.navigationItem.hidesBackButton = YES;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"UserFanKui"];

    backMessageField.text= @"";
    backMessageField.scrollEnabled = YES;
    [backMessageField resignFirstResponder];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"UserFanKui"];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
